//
//  SubmitLoadingView.m
//  SmartWatch
//
//  Created by zwj on 2022/11/25.
//

#import "SubmitLoadingView.h"
#import "YDCircleProgressView.h"

@interface SubmitLoadingView ()
@property (nonatomic, strong) YDCircleProgressView *progressView;
@property(nonatomic,strong) UILabel *name;
@property(nonatomic,strong) dispatch_source_t timer;
@end


@implementation SubmitLoadingView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = COLOR_4;
        self.layer.cornerRadius = 10;
        
        MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
        root.myHorzMargin = 0;
        root.gravity = MyGravity_Center;
        root.myHeight = frame.size.height;
        root.subviewVSpace = 20;
        [self addSubview:root];
        
        self.progressView = [[YDCircleProgressView alloc] initWithFrame:CGRectMake((frame.size.width - 100)/2, 36, 100, 100)];
        self.progressView.mySize = CGSizeMake(100, 100);
        _progressView.circleRadius = 40;
        _progressView.circleBorderWidth = 8;
        _progressView.circleColor = [UIColor whiteColor];
        _progressView.progressColor = [UIColor greenColor];
        _progressView.pointRadius = 4;
        _progressView.pointColor = [UIColor greenColor];
        _progressView.pointBorderWidth = 0.5;
        _progressView.pointBorderColor = [UIColor greenColor];
        [root addSubview:_progressView];
        _progressView.curProgress = 0.0;
        
        
        WeakSelf
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(self.timer,DISPATCH_TIME_NOW,1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(self.timer, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.progressView updateProgress:1.0 duration:1.0 completion:^{
                    weakSelf.progressView.curProgress = 0.0;
                }];
               
            });
        });
        dispatch_resume(self.timer);
        
        UILabel *name = [UILabel createLabelText:@"正在向健康大数据系统提交用户样本数据，请勿关闭" font:UIFontMake(16) textColor:COLOR1515];
        name.myHeight = kAutoHeight;
        name.myHorzMargin = 15;
        name.textAlignment = NSTextAlignmentCenter;
        [root addSubview:name];
        self.name = name;
    }
    return self;
}
- (void)dealloc {
    dispatch_source_cancel(self.timer);
}


- (void)setText:(NSString *)text {
    self.name.text = text;
}
@end
